package com.cloud.form.interfaces;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.cloud.form.api.bean.WfActDef;
import com.cloud.form.api.bean.WfProcDef;
import com.cloud.form.common.config.FeignConfiguration;

import feign.hystrix.FallbackFactory;

@FeignClient(value = "FREEFLOW", configuration = FeignConfiguration.class, fallbackFactory = FeignClientFreeFlowFallbackFactory.class)
public interface FeignClientFreeflow {

	@RequestMapping(value = "freeflow/api/interface/listprocbybiz", method = RequestMethod.GET)
	List<WfProcDef> listProcDef(@RequestParam("bizId") long bizId);
	
	@RequestMapping(value = "freeflow/api/interface/loadactdefbyproc", method = RequestMethod.GET)
	public List<WfActDef> loadActDefByProc(@RequestParam("procDefId") Long procDefId);
}

@Component
class FeignClientFreeFlowFallbackFactory implements FallbackFactory<FeignClientFreeflow> {
	private static final Logger LOGGER = LoggerFactory.getLogger(FeignClientFreeFlowFallbackFactory.class);

	@Override
	public FeignClientFreeflow create(Throwable cause) {
		return new FeignClientFreeflow() {
			@Override
			public List<WfProcDef> listProcDef(long bizId) {
				if (FeignClientFreeFlowFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientFreeFlowFallbackFactory.LOGGER.debug("listProc fallback; reason was:" + cause);
				}
				return new ArrayList<WfProcDef>();
			}

			@Override
			public List<WfActDef> loadActDefByProc(Long procDefId) {
				if (FeignClientFreeFlowFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientFreeFlowFallbackFactory.LOGGER.debug("loadActDefByProc fallback; reason was:" + cause);
				}
				return null;
			}
		};
	}

}
